package com.yuju.controller.order;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yuju.common.PageModel;
import com.yuju.common.Result;
import com.yuju.common.fileld.validator.FieldValidator;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.hotel.enums.FloorReturnCodeEnum;
import com.yuju.order.enums.ReserveReturnCodeEnum;
import com.yuju.order.enums.ReserveRoomReturnCodeEnum;
import com.yuju.order.model.ReserveModel;
import com.yuju.order.model.ReserveRoomModel;
import com.yuju.order.service.IReserverService;
import com.yuju.util.Utils;
import com.yuju.vo.order.ReserveVO;
/**
 * 
 * 类名：reserveController  <br />
 *
 * 功能：
 *
 * @author : sunyl <br />
 * @Date : 2017年11月24日 上午9:40:05  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("reserve")
public class ReserveController extends BaseController<ReserveVO, ReserveModel> {
	@Autowired
	private IReserverService reserveService;
	/**
	 * 
	 * 功能：<br/>预定的列表
	 *@param myLike_reserveName 预定人
	 *@param myLike_mobile 手机号
	 *@param checkinType 入住类型
	 *@param startTime 预抵开始时间
	 *@param endTime 预抵结束时间
	 *@param reserveStatus 订单状态
	 * @author sunyl
	 * @Date 2017年12月5日 下午2:24:09 <br/>
	 */
	@RequestMapping("list")
	@ResponseBody
	public Result list(@RequestBody JSONObject jsonObject)   {
		ReserveVO reserveVO = jsonObject.toJavaObject(ReserveVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(reserveVO, "pager", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		PageModel pageModel = reserveService.pageList(convert(reserveVO), reserveVO.getPage(), reserveVO.getRows());
		if (Utils.isNotNull(pageModel) && Utils.isNotNull(pageModel.getRows())) {
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(ReserveReturnCodeEnum.RESERVE_QUERY_SUCCESS.getCode());
			result.setReturnMsg(ReserveReturnCodeEnum.RESERVE_QUERY_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	/**
	 * 包含预定的房间信息
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @Date 2017年12月16日 下午1:36:00 <br/>
	 */
	@RequestMapping("listInfo")
	@ResponseBody
	public Result listInfo(@RequestBody JSONObject jsonObject)   {
		ReserveVO reserveVO = jsonObject.toJavaObject(ReserveVO.class);
		String hotel = jsonObject.getString("hotel");
		String groupFlag = jsonObject.getString("groupFlag");
		if(groupFlag!=null){
			reserveVO.setHotelId(hotel);
		}
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(reserveVO, "pager", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		PageModel pageModel = reserveService.listInfo(convert(reserveVO), reserveVO.getPage(), reserveVO.getRows());
		if (Utils.isNotNull(pageModel) && Utils.isNotNull(pageModel.getRows())) {
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(ReserveReturnCodeEnum.RESERVE_QUERY_SUCCESS.getCode());
			result.setReturnMsg(ReserveReturnCodeEnum.RESERVE_QUERY_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	/**
	 * 包含预定的房间信息
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @Date 2017年12月16日 下午1:36:00 <br/>
	 */
	@RequestMapping("listTable")
	@ResponseBody
	public Result listTable(@RequestBody JSONObject jsonObject)   {
		ReserveVO reserveVO = jsonObject.toJavaObject(ReserveVO.class);
		String hotel = jsonObject.getString("hotel");
		String groupFlag = jsonObject.getString("groupFlag");
		if(groupFlag!=null){
			reserveVO.setHotelId(hotel);
		}
		if (Utils.isNull(reserveVO.getBusinessTime())) {
			reserveVO.setBusinessTime(new Date());
		}
		return reserveService.listTable(convert(reserveVO));
	}
	/**
	 * 通过预订单id获取预定的房间列表
	 * 功能：<br/>
	 * reserveId
	 * @author sunyl
	 * @Date 2017年12月15日 下午7:41:58 <br/>
	 */
	@RequestMapping("listReserveRoomModelByReserveId")
	@ResponseBody
	public Result listReserveRoomModelByReserveId(@RequestBody JSONObject jsonObject)   {
		String reserveId = jsonObject.getString("reserveId");
		// 参数校验
		Map<String,String> msg = new HashMap<>();
		if (reserveId ==null ) {
			msg.put("reserveId", "message:'不能为空'");
			return validateError(msg);
		}
		Result result = new Result();
		List<ReserveRoomModel> finds  = reserveService.listReserveRoomModelByReserveId(reserveId);
		if (Utils.isNotNull(finds) && finds.size() > 0) {
			result.setData(finds);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(ReserveRoomReturnCodeEnum.RESERVE_ROOM_QUERY_SUCCESS.getCode());
			result.setReturnMsg(ReserveRoomReturnCodeEnum.RESERVE_ROOM_QUERY_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	/**
	 * 通过房间id获取预定的详细信息
	 * 功能：<br/>
	 * reserveId
	 * @author sunyl
	 * @Date 2017年12月15日 下午7:41:58 <br/>
	 */
	@RequestMapping("getReserveInfoByReserveId")
	@ResponseBody
	public Result getReserveInfoByReserveId(@RequestBody JSONObject jsonObject)   {
		String reserveId = jsonObject.getString("reserveId");
		// 参数校验
		Map<String,String> msg = new HashMap<>();
		if (reserveId == null ) {
			msg.put("reserveId", "message:'不能为空'");
			return validateError(msg);
		}
		Result result = new Result();
		Map<String,Object>  find = reserveService.getReserveInfoByReserveId(reserveId);
		if (Utils.isNotNull(find)) {
			result.setData(find);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(ReserveReturnCodeEnum.RESERVE_QUERY_SUCCESS.getCode());
			result.setReturnMsg(ReserveReturnCodeEnum.RESERVE_QUERY_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	/**
	 * 
	 * 功能：<br/>
	 * 新增或者修改预订单
	 *	@param reserveId 预定主键
	 *	@param reserveVO 预定form表单
	 *	@param listReserveRoomModel 预定排放的list表单
	 * @author sunyl
	 * @Date 2017年12月5日 下午2:22:31 <br/>
	 */
	@RequestMapping("edit")
	@ResponseBody
	public Result edit(@RequestBody JSONObject jsonObject) {
		/*1 接收参数*/
		String hotelId = jsonObject.getString("hotelId");
		String groupId = jsonObject.getString("groupId");
		String hotelCode = jsonObject.getString("hotelCode");
		
		ReserveVO reserveVO = jsonObject.getObject("reserve",ReserveVO.class);
		if(reserveVO.getHotelId() == null){
			reserveVO.setHotelId(hotelId);
		}
		reserveVO.setGroupId(groupId);
		reserveVO.setHotelCode(hotelCode);
		JSONArray arrays = jsonObject.getJSONArray("listReserveRoom");
        String jsonText = arrays.toJSONString();
        List<ReserveVO> listReserveRoomVO = JSON.parseObject(jsonText, 
        		new TypeReference<ArrayList<ReserveVO>>(){});
        
		/*2 参数校验*/
		Map<String, String> fail = FieldValidator.validateAll(reserveVO, "save", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		for(ReserveVO reserveRoomVO:listReserveRoomVO){
			Map<String, String> failForReserveRoomVo = FieldValidator.validateAll(reserveRoomVO, "reserveRoom", true);
			if (!failForReserveRoomVo.isEmpty()) {
				return validateError(failForReserveRoomVo);
			}
		}
		/*3 存入或者更新*/
		List<ReserveModel> reserveModelList = convert(listReserveRoomVO);
		String id = null;
		if(reserveVO.getId() != null){
			id = reserveService.updateReserveOrder(convert(reserveVO), reserveModelList);
		}else{
			id = reserveService.creatReserveOrder(convert(reserveVO),reserveModelList);
		}
		Result result = new Result();
		if (id != null) {
			result.setData(id);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(ReserveReturnCodeEnum.RESERVE_SAVE_SUCCESS.getCode());
			result.setReturnMsg(ReserveReturnCodeEnum.RESERVE_SAVE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReserveReturnCodeEnum.RESERVE_SAVE_FAILURE.getCode());
			result.setReturnMsg(ReserveReturnCodeEnum.RESERVE_SAVE_FAILURE.getDesc());
		}
		return result;
	}
	@RequestMapping("delete")
	@ResponseBody
	public Result delete(@RequestBody JSONObject jsonObject) {
		ReserveVO reserveVO = jsonObject.toJavaObject(ReserveVO.class);
		/*2 参数校验*/
		Map<String, String> fail = FieldValidator.validateAll(reserveVO, "del", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int rows = reserveService.del(reserveVO.getId());
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(ReserveReturnCodeEnum.RESERVE_DEL_SUCCESS.getCode());
			result.setReturnMsg(ReserveReturnCodeEnum.RESERVE_DEL_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReserveReturnCodeEnum.RESERVE_DEL_FAILURE.getCode());
			result.setReturnMsg(ReserveReturnCodeEnum.RESERVE_DEL_FAILURE.getDesc());
		}
		return result;
	}

	@RequestMapping("get")
	@ResponseBody
	public Result get(@RequestBody JSONObject jsonObject) {
		ReserveVO reserveVO = jsonObject.toJavaObject(ReserveVO.class);
		Result result = new Result();
		ReserveModel ReserveModel = reserveService.find(convert(reserveVO));
		if (Utils.isNotNull(ReserveModel)) {
			result.setData(ReserveModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(ReserveReturnCodeEnum.RESERVE_QUERY_SUCCESS.getCode());
			result.setReturnMsg(ReserveReturnCodeEnum.RESERVE_QUERY_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	/**
	 * 功能：<br/>
	 *  取消预定
	 * @param id reserve的主键
	 *
	 * @author sunyl
	 * @Date 2017年12月5日 下午8:02:35 <br/>
	 */
    @RequestMapping("cancelReserve")
    @ResponseBody
    public Result cancelReserve(@RequestBody JSONObject jsonObject){
    	ReserveVO reserveVO = jsonObject.toJavaObject(ReserveVO.class);
    	Map<String, String> fail = FieldValidator.validateAll(reserveVO, "del", true);
    	if (!fail.isEmpty()) {
    		return validateError(fail);
    	}
    	Result result = new Result();
		int rows = reserveService.cancelReserveOrder(reserveVO.getId());
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(ReserveReturnCodeEnum.RESERVE_CANCEL_SUCCESS.getCode());
			result.setReturnMsg(ReserveReturnCodeEnum.RESERVE_CANCEL_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReserveReturnCodeEnum.RESERVE_CANCEL_FAILURE.getCode());
			result.setReturnMsg(ReserveReturnCodeEnum.RESERVE_CANCEL_FAILURE.getDesc());
		}
		return result;
    }
    /**
     * 
     * 功能：<br/>恢复预订单
     * @param id reserve的主键
     * @author sunyl
     * @Date 2017年12月5日 下午8:01:41 <br/>
     */
    @RequestMapping("recoverReserve")
    @ResponseBody
    public Result recoverReserve(@RequestBody JSONObject jsonObject){
    	ReserveVO reserveVO = jsonObject.toJavaObject(ReserveVO.class);
    	Map<String, String> fail = FieldValidator.validateAll(reserveVO, "del", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int rows = reserveService.recoverReserveOrder(reserveVO.getId());
    	if (rows > 0) {
    		result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
    		result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
    		result.setReturnCode(ReserveReturnCodeEnum.RESERVE_RECOVER_SUCCESS.getCode());
    		result.setReturnMsg(ReserveReturnCodeEnum.RESERVE_RECOVER_SUCCESS.getDesc());
    	} else {
    		result.setStatus(ReturnStatusEnum.FAILURE.getCode());
    		result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
    		result.setReturnCode(ReserveReturnCodeEnum.RESERVE_RECOVER_FAILURE.getCode());
    		result.setReturnMsg(ReserveReturnCodeEnum.RESERVE_RECOVER_FAILURE.getDesc());
    	}
    	return result;
    }
    
}
